package com.ruoyi.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.service.ITypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类名Controller
 *
 * @author handsome
 * @date 2023-12-19
 */
@RestController
@RequestMapping("/system/type")
public class TypeController extends BaseController {
    @Resource(name = "typeService")
    private ITypeService typeService;

    /**
     * 查询分类名列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(Type type) {
        startPage();
        List<Type> list = typeService.selectTypeList(type);
        return getDataTable(list);
    }


    @GetMapping("/num")
    public AjaxResult getTypeNum() {
        return AjaxResult.success(typeService.selectTypeNum());
    }

    /**
     * 导出分类名列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "分类名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Type type) {
        List<Type> list = typeService.selectTypeList(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "分类名数据");
    }

    /**
     * 获取分类名详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{typeID}")
    public AjaxResult getInfo(@PathVariable("typeID") Long typeID) {
        return success(typeService.selectTypeByTypeID(typeID));
    }

    /**
     * 新增分类名
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "分类名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Type type) {
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改分类名
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "分类名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Type type) {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除分类名
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "分类名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIDs}")
    public AjaxResult remove(@PathVariable Long[] typeIDs) {
        return toAjax(typeService.deleteTypeByTypeIDs(typeIDs));
    }
}
